package com.taplinker.client.im.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.taplinker.client.BaseActivity;
import com.taplinker.client.R;
import com.taplinker.client.domain.MenuDomain;
import com.taplinker.client.im.adapter.QueueListAdapter;
import com.taplinker.client.im.domain.EnumConversationSource;
import com.taplinker.client.im.domain.ImDataCenter;
import com.taplinker.client.im.protocol.TapLinkerConversation;
import com.winona.Base1Activity;
import com.winona.HomeActivity;

@SuppressLint("ResourceAsColor")
public class ChatIMQueueActivity extends Base1Activity {
	private TextView titleText;

	private TextView privateBt;
	// private TextView publicBt;
	private TextView privateQueue;
	private TextView publicQueue;
	private ListView mListView;
	private View mFooter;
	private TextView footerTv;
	private ProgressBar footerProgress;

	private QueueListAdapter mAdapter;
	private String filter = null;
	private PopupWindow filterPopwin;
	private boolean mHasMore = true;

	private ImageView left_back;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.chat_queue);
		init();
		resetNav();
		// initFilterPopWin();
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
		// //resetNav();
	}

	@Override
	protected void onResume() {
		super.onResume();
		mAdapter.notifyDataSetChanged();
	}

	public void init() {
		titleText = (TextView) findViewById(R.id.title);

		titleText.setText("门诊记录");

		// publicBt = (TextView) findViewById(R.id.publicQueues);
		privateBt = (TextView) findViewById(R.id.privateQueues);

		mListView = (ListView) findViewById(R.id.frame_listview_tweet);
		mFooter = getLayoutInflater().inflate(R.layout.listview_footer, null);
		footerTv = (TextView) mFooter.findViewById(R.id.listview_foot_more);
		left_back = (ImageView) findViewById(R.id.title_left_bt);

		left_back.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				startActivity(ChatActivity.class);

			}
		});

		footerProgress = (ProgressBar) mFooter
				.findViewById(R.id.listview_foot_progress);

		mFooter.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (mHasMore) {
					mFooter.setVisibility(View.VISIBLE);
					if (footerProgress.getVisibility() == View.VISIBLE) {
						return;
					}
					footerTv.setText("加载中......");
					footerProgress.setVisibility(View.VISIBLE);
					ImDataCenter.getInstance().loadMore(filter);
				}
			}
		});

		mListView.addFooterView(mFooter);
		if (ImDataCenter.getInstance().isPublicQueue()) {
			mAdapter = new QueueListAdapter(this, ImDataCenter.getInstance()
					.getPubQueue());
		} else {
			mAdapter = new QueueListAdapter(this, ImDataCenter.getInstance()
					.getPrivQueue());
		}
		mListView.setAdapter(mAdapter);
		// resetNav();
		mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(ChatIMQueueActivity.this,
						ChatQueueActivity.class);
				TapLinkerConversation con = (TapLinkerConversation) mAdapter
						.getItem(position);
				EnumConversationSource src = EnumConversationSource
						.getByCode(con.getSource());
				ImDataCenter.getInstance().setConversation(con);
				startActivity(intent);
			}
		});
		// // 患者
		//
		// publicBt.setOnClickListener(new View.OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// if (!ImDataCenter.getInstance().isPublicQueue()) {
		// ImDataCenter.getInstance().setPublicQueue(true);
		// resetNav();
		// MenuDomain.open(MenuDomain.MenuItems.PUBLIC);
		// // refreshMenuSelected();
		// publicBt.setBackgroundResource(R.drawable.asklight);
		// privateBt.setBackgroundResource(R.drawable.asknormal);
		//
		// publicBt.setTextColor(R.color.red);
		// privateBt.setTextColor(R.color.white);
		// }
		// }
		// });
		//
		// privateBt.setOnClickListener(new View.OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// if (ImDataCenter.getInstance().isPublicQueue()) {
		// ImDataCenter.getInstance().setPublicQueue(false);
		// resetNav();
		// MenuDomain.open(MenuDomain.MenuItems.PRIVATE);
		// // privateBt.setBackgroundResource(R.drawable.asklightright);
		// // publicBt.setBackgroundResource(R.drawable.asknormalleft);
		//
		// // privateBt.setTextColor(R.color.red);
		// // publicBt.setTextColor(R.color.white);
		// }
		// }
		// });
	}

	class FilterItemOnClickListener implements View.OnClickListener {
		EnumConversationSource source;

		public FilterItemOnClickListener(EnumConversationSource source) {
			super();
			this.source = source;
		}

		@Override
		public void onClick(View v) {
			if (source != null) {
				reLoad(source.getCode());
			} else {
				reLoad(null);
			}
			filterPopwin.dismiss();
		}

	}

	private void resetNav() {
		if (ImDataCenter.getInstance().isPublicQueue()) {
			// titleText.setText("咨询队列");
			// privateBt.setSelected(false);
			// publicBt.setSelected(true);
			resetAdapter(true);
		} else {
			// titleText.setText("我的会话");
			// publicBt.setSelected(false);
			// privateBt.setSelected(true);
			resetAdapter(false);
		}
	}

	private void reLoad(String source) {
		filter = source;
		ImDataCenter.getInstance().reloadQueue(source);
	}

	private void resetAdapter(boolean pub) {
		mHasMore = true;
		footerTv.setText("加载更多");
		footerProgress.setVisibility(View.GONE);
		ImDataCenter.getInstance().setPublicQueue(pub);
		// if (pub) {
		// mAdapter.setData(ImDataCenter.getInstance().getPubQueue());
		// } else {
		mAdapter.setData(ImDataCenter.getInstance().getPrivQueue());
		// }
		mAdapter.notifyDataSetChanged();
		mListView.setSelection(0);
	}

	@Override
	public void handleMessage(String type) {
		super.handleMessage(type);
		if (type.equals(ImDataCenter.MENU_REFRESH)) {
			int puCount = ImDataCenter.getInstance().getPubNum();
			int prCount = ImDataCenter.getInstance().getPriNum();
			if (puCount > 0) {
				// publicCountTv.setVisibility(View.VISIBLE);
				// publicCountTv.setText(String.valueOf(puCount));
			} else {
				// publicCountTv.setVisibility(View.GONE);
			}
			if (prCount > 0) {
				// privateCountTv.setVisibility(View.VISIBLE);
				// privateCountTv.setText(String.valueOf(prCount));
			} else {
				// privateCountTv.setVisibility(View.GONE);
			}
		}
		if (type.equals(ImDataCenter.QUEUE_REFRESH)) {
			mAdapter.notifyDataSetChanged();
			footerTv.setText("加载更多");
			footerProgress.setVisibility(View.GONE);
		}
		if (type.equals(ImDataCenter.CONVERSATION_NO_MORE)) {
			footerTv.setText("加载完成");
			mHasMore = false;
		} else {
			footerTv.setText("加载更多");
			mHasMore = true;
		}
	}

	static class InnerHandler extends Handler {

	}
}
